package util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * Created by ilioili on 2015/1/26.
 */
public class NetUtil {
    /**
     * 检测网络是否可用
     *
     * @return
     */
    public static boolean isNetworkConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        return ni != null && ni.isConnectedOrConnecting();
    }

    public Type getNetworkType(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo == null) {
            return Type.NO_CONNECTION;
        }
        int nType = networkInfo.getType();
        switch (networkInfo.getType()) {
            case ConnectivityManager.TYPE_WIFI:
                return Type.WIFI;
            default:
                return Type.HONEYCOMB;
        }
        //        if(nType==ConnectivityManager.TYPE_WIFI)
        //        if (nType == ConnectivityManager.TYPE_MOBILE) {
        //            return TYPE.
        //            String extraInfo = networkInfo.getExtraInfo();
        //            if(extraInfo!=null){
        //                if (extraInfo.toLowerCase().equals("cmnet")) {
        //                    netType = NETTYPE_CMNET;
        //                } else {
        //                    netType = NETTYPE_CMWAP;
        //                }
        //            }
        //        } else if (nType == ConnectivityManager.TYPE_WIFI) {
        //            netType = NETTYPE_WIFI;
        //        }
        //        return netType;
    }

    public enum Type {NO_CONNECTION, WIFI, HONEYCOMB}
}
